package com.tiantian.judge.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.tiantian.common.core.domain.ResponseResult;
import com.tiantian.common.core.domain.page.TableDataInfo;
import com.tiantian.common.core.exception.BusinessException;
import com.tiantian.common.log.annotation.Log;
import com.tiantian.common.log.enums.BusinessType;
import com.tiantian.common.log.enums.OperatorType;
import com.tiantian.common.satoken.domain.controller.BaseController;
import com.tiantian.judge.domain.bo.JudgeScoreBo;
import com.tiantian.judge.domain.dto.QueryReplySubDto;
import com.tiantian.judge.domain.vo.JudgeDetailVo;
import com.tiantian.judge.domain.vo.JudgeVo;
import com.tiantian.judge.domain.vo.SubmitInfoVo;
import com.tiantian.judge.service.JudgeService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 判题
 *
 * @author TianTian
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/judge")
public class SysJudgeController extends BaseController {


    private final JudgeService judgeService;

    /**
     * 分页查询判题列表
     */
    @GetMapping("/list")
    @SaCheckPermission("judge")
    public TableDataInfo getJudgeList(JudgeVo judgeVO) {
        startPage();
        // 判查询选参数是否为[我需要判的题]
        if (judgeVO.getNeedMe() != null && judgeVO.getNeedMe()) {
            judgeVO.setJudger(getNickName());
        }
        return getDataTable(judgeService.getJudgeList(judgeVO));
    }

    /**
     * 根据用户ID查询判题信息
     */
    @GetMapping("/info/{userId}")
    @SaCheckPermission("judge")
    public ResponseResult<JudgeVo> getJudgeInfo(@PathVariable Long userId) {
        return ResponseResult.ok(judgeService.getJudgeInfoByUserId(userId));
    }

    /**
     * 根据用户ID查询判断详细信息列表
     *
     * @param userId 用户ID
     * @return 答题信息列表
     */
    @GetMapping("/detailList/{userId}")
    @SaCheckPermission("judge")
    public ResponseResult<List<JudgeDetailVo>> getJudgeDetailList(@PathVariable Long userId) {
        List<JudgeDetailVo> sysJudgeDetailVos = judgeService.getJudgeDetailListByUserId(userId);
        return ResponseResult.ok(sysJudgeDetailVos);
    }

    /**
     * 查询判题信息我的
     */
    @GetMapping("/info/me")
    @SaCheckPermission("user")
    public ResponseResult<JudgeVo> getMyJudgeInfo() {
        Long userId = getUserId();
        return ResponseResult.ok(judgeService.getJudgeInfoByUserId(userId));
    }

    /**
     * 查询我的判断详细信息列表
     */
    @GetMapping("/detailList/me")
    @SaCheckPermission("user")
    public ResponseResult<List<JudgeDetailVo>> getMyJudgeDetailList() {
        Long userId = getUserId();
        List<JudgeDetailVo> sysJudgeDetailVos = judgeService.getJudgeDetailListByUserId(userId);
        return ResponseResult.ok(sysJudgeDetailVos);
    }

    /**
     * 获取提交信息
     *
     * @param queryReplySubDTO 查询参数
     */
    @GetMapping("/subInfo")
    @SaCheckPermission("judge")
    public TableDataInfo getSubInfo(QueryReplySubDto queryReplySubDTO) {
        startPage();
        List<SubmitInfoVo> submitInfoVOList = judgeService.getSubInfo(queryReplySubDTO);
        return getDataTable(submitInfoVOList);
    }

    /**
     * 获取提交的总信息
     */
    @GetMapping("/subTotal")
    @SaCheckPermission("judge")
    public ResponseResult<Map<String, Object>> getSubTotal() {
        Map<String, Object> ajax = judgeService.getTotalInfoSubmitted();
        return ResponseResult.ok(ajax);
    }

    /**
     * 批量修改分数
     *
     * @param sysJudgeScoreList 评分集合
     * @return 评分信息
     */
    @PostMapping("/setScore")
    @SaCheckPermission("judge")
    @Log(businessType = BusinessType.JUDGE_TOPIC, operatorType = OperatorType.ADMIN)
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Map<String, Object>> ManualScoring(@RequestBody List<JudgeScoreBo> sysJudgeScoreList) {
        // 校验参数
        checkParameter(sysJudgeScoreList);
        Map<String, Object> ajax = judgeService.ManualScoring(sysJudgeScoreList);
        return ResponseResult.ok(ajax);
    }

    /**
     * 参数校验
     */
    private void checkParameter(List<JudgeScoreBo> sysJudgeScoreList) {
        if (CollUtil.isEmpty(sysJudgeScoreList)) {
            throw new BusinessException("非法参数");
        }
        for (JudgeScoreBo sysJudgeScoreBo : sysJudgeScoreList) {
            // 如果两个参数中有一个为空
            if (!StrUtil.isAllNotEmpty(sysJudgeScoreBo.getReplyId(), sysJudgeScoreBo.getScore().toString())) {
                throw new BusinessException("非法参数");
            }
        }
        // 判断是否有题目超过最高分
    }
}
